;(function($){
	$.frontspa_jdh = $.frontspa_jdh || {};
	$.fn.frontspa_jdh = function(opts){
		var el = this;
		var opt = {
			jdh : "",
			ds : [],
			wait : function(){},
			goon : function(){},
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.frontspa_jdh = $.extend($.frontspa_jdh,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//选择酒店
	    	el.find(".hotel-item").unbind("click").click(function(){
//	    		el.find(".hotel-item").removeClass("choosen").removeClass("blue").removeClass("bolder")
	    		el.find(".choosen").removeClass("choosen")
	    		el.find(".blue").removeClass("blue")
	    		el.find(".bolder").removeClass("bolder")
    			$(this).addClass("choosen").addClass("blue").addClass("bolder")
	    		var id=$(this).attr("id")
	    		var ds = $.parseJSON($(this).find(".json").html())
//	    		t.setJdhDetailInfo(ds);
    			param.jdh=id;
				param.dataBack(param.jdh);
	    	})
	    	
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.find("#pagination").empty();
			el.find("#jdhList").empty();
			var data = param.ds;
			 
			var selJdh = 1;
			var jdh=param.jdh;
			
			$(data).each(function(k,v){
				v.seq = k + 1;
				if(v.sele==1){
					jdh = v.jdh;
					param.jdh = v.jdh;
					selJdh = k+1;
				}
			})
			
			if(data.length>0){
				var containerWidth=el.width()
				var quan=Math.floor(containerWidth/220)*2
				var total=Math.ceil((data.length)/quan)
				var showResult = Math.ceil(selJdh/quan)
				var current = Math.ceil(selJdh/quan);
				el.find("#jdhList").empty();
				var lower = (showResult-1)*quan
				var upper = (showResult)*quan
				if(showResult*quan>data.length){
					upper = data.length;//%quan;
				}
				var index = (selJdh%quan);
				if(index==0){
					index = quan;
				}
				
				for(var i=lower;i<upper;i++){
					var key = i;
					var value = data[i];
					var choosen = ""
					if(value.sele==1){
						choosen = " choosen blue bolder"
//						t.setJdhDetailInfo(value);
					}
					var html =	'<div class="hotel-item'+choosen+'" data-jdh="'+value.jdh+'" title="'+$.i18n.propJsp("金额:")+value.xfje+'\n'+$.i18n.propJsp("数量:")+value.sl+'" id="'+value.jdh+'">'+
									'<div class="json hide">'+JSON.stringify(value)+'</div>'+
									value.info+
								'</div>';
					el.find("#jdhList").append(html);
				}
				
				
				t.bindEvent();
//				if(param.jdh){
//					el.find(".hotel-item:eq("+(index-1)+")").trigger("click");
//				}
				//初始化翻页
				el.find('#pagination').pagination({
					totalPage: total,
				    pageSize: quan,
				    current : current,
				    showResult:2,
				    resultCalculate:data.length,
				    callback: function(json, pagination){
				    	el.find("#jdhList").empty()
						var afterData=[]
						$(data).each(function(k,v){
							var start=((json-1)*quan)
							var end=(json*quan)
							if(k>=start&&k<end){//
								afterData.push(v)
							}
						})
				    	$(afterData).each(function(key,value){
				    		var cla = "";
				    		if(param.jdh==value.jdh){
				    			cla = " blue bolder"
				    		}
				    		var choosen = ""
								if(value.sele==1){
									choosen = " choosen blue bolder"
//									t.setJdhDetailInfo(value);
								}
							var html =	'<div class="hotel-item '+choosen+'" data-jdh="'+value.jdh+'" title="'+$.i18n.propJsp("金额:")+value.xfje+'\n'+$.i18n.propJsp("数量:")+value.sl+'" id="'+value.jdh+'">'+
											'<div class="json hide">'+JSON.stringify(value)+'</div>'+
											value.info+
										'</div>';
							el.find("#jdhList").append(html);
						})
						t.bindEvent();
				    }
				})
				
			}
		},
//		setJdhDetailInfo : function(ds){
//			
//			$(".jdh-detail-info").each(function(k,v){
//				$(v).text(ds[v.id]||"")
//			})
//		}
	}
})(jQuery);